package com.cw.demo.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * TODO
 *
 * @author chenw
 * @date 2021/1/3
 */
public class ProxyTest {

    public static void main(String[] args) {

        //staticProxy();

        dynamicProxy();
    }
    /**
     * 动态代理
     */
    private static void dynamicProxy() {
        Student student = new Student("李四");
        //创建一个与代理对象相关的InvocationHandler
        InvocationHandler invocationHandler = new StuInvocationHandler<Person>(student);
        //创建一个代理对象stuProxy来代理李四，代理对象的每个执行方法都会替换执行invocation中的invoke方法
        Person stuProxy = (Person)Proxy.newProxyInstance(Person.class.getClassLoader(),new Class[]{Person.class},invocationHandler);
        stuProxy.talk("感谢老师们辛苦的付出");
    }
    /**
     * 静态代理
     */
    private static void staticProxy() {
        Student student = new Student("张三");
        StudentProxy studentProxy = new StudentProxy(student);
        studentProxy.talk("感谢老师们辛苦的付出");
    }
}
